{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2407631d",
   "metadata": {},
   "source": [
    "# 建立API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aebcec60",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import requests\n",
    "# # 下方改为自己的key&secret\n",
    "# api_key = \"Vf1TVr0kb7C0wavMhpaKym9OSkwGRNaw\"\n",
    "# api_secret = \"\tPOyLi052sYdsVnEe-Gs27AxP9fqd7zHJ\"\n",
    "\n",
    "# detectUrl = \" ~~~ \"\n",
    "\n",
    "# payload = {\n",
    "#     \"api_key\": api_key,\n",
    "#     \"api_secret\": api_secret,\n",
    "#     \"image_url\": \" 图片链接 \"\n",
    "# }\n",
    "\n",
    "# r = requests.post(detectUrl, payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d20101c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3d172147",
   "metadata": {},
   "outputs": [],
   "source": [
    "#r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1569b0f",
   "metadata": {},
   "source": [
    "# 关于Face++的API调用练习位于week2的py文章中，week2包含Face++所有的人脸检测API调用\n",
    "# 数据处理小魔法 pandas.json_normalize() 实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4fe058ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 1. url\n",
    "analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = \"Vf1TVr0kb7C0wavMhpaKym9OSkwGRNaw\"\n",
    "API_sercret = \"POyLi052sYdsVnEe-Gs27AxP9fqd7zHJ\"\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "face_token = \"98e8c520c5a2fa8fe37275ef8f8b1b24\"\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':face_token,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = analyze_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "267b2e18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c954ba40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 154,\n",
       " 'request_id': '1648457870,54f0860b-405e-4cf7-b5a7-3cc7ade30d7d',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.002,\n",
       "     'neutral': 0.189,\n",
       "     'disgust': 99.692,\n",
       "     'anger': 0.002,\n",
       "     'surprise': 0.012,\n",
       "     'fear': 0.054,\n",
       "     'happiness': 0.051},\n",
       "    'beauty': {'female_score': 59.735, 'male_score': 51.794},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 36},\n",
       "    'mouthstatus': {'close': 97.061,\n",
       "     'surgical_mask_or_respirator': 0.1,\n",
       "     'open': 1.699,\n",
       "     'other_occlusion': 1.14},\n",
       "    'glass': {'value': 'Normal'},\n",
       "    'skinstatus': {'dark_circle': 2.567,\n",
       "     'stain': 77.981,\n",
       "     'acne': 60.615,\n",
       "     'health': 2.917},\n",
       "    'headpose': {'yaw_angle': 2.7179992,\n",
       "     'pitch_angle': 14.942439,\n",
       "     'roll_angle': -3.3533483},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.03},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.03},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.03}},\n",
       "    'smile': {'threshold': 50.0, 'value': 2.897},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 96.157,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.001,\n",
       "      'no_glass_eye_open': 0.489,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 3.353},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 98.14,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.086,\n",
       "      'no_glass_eye_open': 1.232,\n",
       "      'normal_glass_eye_close': 0.001,\n",
       "      'dark_glasses': 0.541}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 85.504},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.48,\n",
       "      'vector_z_component': 0.978,\n",
       "      'vector_x_component': 0.008,\n",
       "      'vector_y_component': -0.206,\n",
       "      'position_y_coordinate': 0.352},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.518,\n",
       "      'vector_z_component': 0.968,\n",
       "      'vector_x_component': 0.07,\n",
       "      'vector_y_component': -0.24,\n",
       "      'position_y_coordinate': 0.362}}},\n",
       "   'face_rectangle': {'width': 213, 'top': 213, 'left': 340, 'height': 213},\n",
       "   'face_token': '98e8c520c5a2fa8fe37275ef8f8b1b24'}]}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "358b1d92",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "721078cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>98e8c520c5a2fa8fe37275ef8f8b1b24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>0.189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>99.692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>0.051</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>59.735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>51.794</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>97.061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>1.699</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>1.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>2.567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>77.981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>60.615</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>2.917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>2.717999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>14.942439</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>-3.353348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>2.897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>96.157</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>0.489</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>3.353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>98.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>1.232</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>85.504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.978</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>0.008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.968</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>0.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>213</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          98e8c520c5a2fa8fe37275ef8f8b1b24\n",
       "attributes.emotion.sadness                                                     0.002\n",
       "attributes.emotion.neutral                                                     0.189\n",
       "attributes.emotion.disgust                                                    99.692\n",
       "attributes.emotion.anger                                                       0.002\n",
       "attributes.emotion.surprise                                                    0.012\n",
       "attributes.emotion.fear                                                        0.054\n",
       "attributes.emotion.happiness                                                   0.051\n",
       "attributes.beauty.female_score                                                59.735\n",
       "attributes.beauty.male_score                                                  51.794\n",
       "attributes.gender.value                                                         Male\n",
       "attributes.age.value                                                              36\n",
       "attributes.mouthstatus.close                                                  97.061\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.1\n",
       "attributes.mouthstatus.open                                                    1.699\n",
       "attributes.mouthstatus.other_occlusion                                          1.14\n",
       "attributes.glass.value                                                        Normal\n",
       "attributes.skinstatus.dark_circle                                              2.567\n",
       "attributes.skinstatus.stain                                                   77.981\n",
       "attributes.skinstatus.acne                                                    60.615\n",
       "attributes.skinstatus.health                                                   2.917\n",
       "attributes.headpose.yaw_angle                                               2.717999\n",
       "attributes.headpose.pitch_angle                                            14.942439\n",
       "attributes.headpose.roll_angle                                             -3.353348\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                  0.03\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                                0.03\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                              0.03\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                         2.897\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                            96.157\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                 0.001\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                             0.489\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                              3.353\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                             98.14\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                0.086\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                             1.232\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                             0.001\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                             0.541\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  85.504\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                              0.48\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.978\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                             0.008\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                            -0.206\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.352\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.518\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.968\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                              0.07\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                             -0.24\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.362\n",
       "face_rectangle.width                                                             213\n",
       "face_rectangle.top                                                               213\n",
       "face_rectangle.left                                                              340\n",
       "face_rectangle.height                                                            213"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a0af40c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
